﻿//-----------------------------------------------------------------------
// <copyright file="IsolatedStorageLogReader.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 
namespace ChatBox.Engine.Log
{
    using System.IO;
    using System.IO.IsolatedStorage;

    /// <summary>
    /// log to a file under Isolated Storage
    /// </summary>
    public static class IsolatedStorageLogReader
    {
        /// <summary>
        /// Read the log
        /// </summary>
        /// <returns>the log contents</returns>
        public static string ReadLog()
        {
            string result = string.Empty;

            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForAssembly();

            if (IsolatedStorageLogHelper.StorageFileExists(storage))
            {
                using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(
                    IsolatedStorageLogHelper.StorageFileName,
                    FileMode.Open,
                    FileAccess.Read,
                    storage))
                {
                    using (StreamReader reader = new StreamReader(fileStream))
                    {
                        result = reader.ReadToEnd();
                        reader.Close();
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// Remove the error log contents
        /// </summary>
        public static void ClearLog()
        {
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForAssembly();
            if (IsolatedStorageLogHelper.StorageFileExists(storage))
            {
                storage.DeleteFile(IsolatedStorageLogHelper.StorageFileName);
            }
        }
   }
}